/*
 * TransaksiCustomerView.java
 *
 * Created on July 23, 2008, 12:53 AM
 */
package com.indragunawan.restobiz.app;

import java.awt.event.KeyEvent;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author  igoens
 */
public class TransaksiCustomerView extends javax.swing.JDialog {

    private static final long serialVersionUID = -6347709243325834051L;

    private int i;
    private List customerList;
    private Boolean openInvoice;

    /** Creates new form TransaksiCustomerView */
    public TransaksiCustomerView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        fillCustomerData();
    }

    public String getCustomerValue() {
        String customer;

        if (!customerField.getSelectedItem().toString().isEmpty()) {
            customer = customerField.getSelectedItem().toString();
        } else {
            customer = "";
        }

        return customer;
    }

    private void fillCustomerData() {
        try {
            GeneralConfig cfg = new GeneralConfig();

            // Create list of kelompok
            customerList = cfg.getCustomer();

            // Fill ComboBox with result
            customerField.removeAllItems();
            for (i = 0; i <= customerList.size() - 1; i++) {
                customerField.addItem(customerList.get(i));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(GeneralConfig.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Boolean getConfirmNew() {
        return this.openInvoice;
    }

    public void setConfirmNew(Boolean Value) {
        this.openInvoice = Value;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        customerPanel = new javax.swing.JPanel();
        customerField = new javax.swing.JComboBox();
        customerLabel = new javax.swing.JLabel();
        okButton = new javax.swing.JButton();
        batalButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getResourceMap(TransaksiCustomerView.class);
        setTitle(resourceMap.getString("transaksiCustomer.title")); // NOI18N
        setName("transaksiCustomer"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        customerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("customerPanel.border.title"))); // NOI18N
        customerPanel.setName("customerPanel"); // NOI18N

        customerField.setEditable(true);
        customerField.setName("customerField"); // NOI18N

        customerLabel.setDisplayedMnemonic('C');
        customerLabel.setLabelFor(customerField);
        customerLabel.setText(resourceMap.getString("customerLabel.text")); // NOI18N
        customerLabel.setName("customerLabel"); // NOI18N

        javax.swing.GroupLayout customerPanelLayout = new javax.swing.GroupLayout(customerPanel);
        customerPanel.setLayout(customerPanelLayout);
        customerPanelLayout.setHorizontalGroup(
            customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, customerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerField, 0, 340, Short.MAX_VALUE)
                    .addComponent(customerLabel))
                .addContainerGap())
        );
        customerPanelLayout.setVerticalGroup(
            customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customerLabel)
                .addGap(4, 4, 4)
                .addComponent(customerField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(com.indragunawan.restobiz.app.MainApp.class).getContext().getActionMap(TransaksiCustomerView.class, this);
        okButton.setAction(actionMap.get("okButtonAction")); // NOI18N
        okButton.setMnemonic('O');
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                okButtonKeyPressed(evt);
            }
        });

        batalButton.setAction(actionMap.get("batalButtonAction")); // NOI18N
        batalButton.setMnemonic('B');
        batalButton.setText(resourceMap.getString("batalButton.text")); // NOI18N
        batalButton.setName("batalButton"); // NOI18N
        batalButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                batalButtonKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(customerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(batalButton)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {batalButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(batalButton)
                    .addComponent(okButton))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
    fillCustomerData();
}//GEN-LAST:event_formWindowActivated

private void okButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_okButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        okButtonAction();
    }
}//GEN-LAST:event_okButtonKeyPressed

private void batalButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_batalButtonKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        batalButtonAction();
    }
}//GEN-LAST:event_batalButtonKeyPressed

    @Action
    public void batalButtonAction() {
        setConfirmNew(false);
        dispose();
    }

    @Action
    public void okButtonAction() {
        if (!(customerField.getSelectedItem().toString().isEmpty())) {
            setConfirmNew(true);
            dispose();
        } else {
            JOptionPane.showMessageDialog(null, "Harap isi atau pilih customer!");
            customerField.requestFocus();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton batalButton;
    private javax.swing.JComboBox customerField;
    private javax.swing.JLabel customerLabel;
    private javax.swing.JPanel customerPanel;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
}
